const jwt = require('jsonwebtoken');
const helper = require('../../util/helper');
const config = require('../../config/environment/index');
const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');

/*
  request {
      model_id: 1,
 product_name:'红外感应器'，
 vendor：'鸿雁'，
  }
 */


exports.add = async(ctx) => {
  let vendor_id = ctx.request.body.vendor_id || '';
  let product_name = ctx.request.body.product_name || '';
  let update_time = moment().format('YYYY-MM-DD HH:mm:ss');
  let model_type_id = ctx.request.body.model_type_id || '';
  let model_name = ctx.request.body.model_name || '';
  let product_type = ctx.request.body.product_type || '';
  let icon_id = ctx.request.body.icon_id || '';
  let create_time = moment().format('YYYY-MM-DD HH:mm:ss');

  let newproduct = {
    vendor_id,
    product_name,
    model_type_id,
    model_name,
    update_time,
    // model_property,
    product_type,
    icon_id,
    create_time
  };
  try {
  let product = await mysql.query(`INSERT INTO product SET ?`, newproduct);
  ctx.body = {
    return: 200,
    product: product.insertId,
    message: "新增product成功"
  };
  }catch (error) {

    let vendor_ids = await mysql.query(`select vendor_id from model `);
    if (vendor_ids && vendor_ids.indexOf({vendor_id}) === -1) {
      ctx.body = {
        return: 201,
        message: `vendor 中 不存在 vendor_id = ${vendor_id}的 设备, 或已被删除`,
      };
      return;
    }
    let model_type_ids = await mysql.query(`select model_type_id from model_type `);
    if (model_type_ids && model_type_ids.indexOf({model_type_id}) === -1) {
      ctx.body = {
        return: 201,
        message: `model_type 中 不存在 model_type_id = ${model_type_id}的 设备, 或已被删除`,
      };
      return;
    }
    let icon_ids = await mysql.query(`select icon_id from icon `);
    if (icon_ids && icon_ids.indexOf({icon_id}) === -1) {
      ctx.body = {
        return: 201,
        message: `icon 中 不存在 vendor_id = ${icon_id}的 设备, 或已被删除`,
      };
      return;
    }
    console.log(error);
    ctx.body = {
      return: 202,
      message: '新增时出现错误',
    };
  }
}


exports.getList = async(ctx) => {
  let params = ctx.query;
  let counts = params.counts || 10;
  let indexfrom = params.indexfrom || 0;

  let results = await mysql.query(`SELECT * FROM product  ORDER BY product_id LIMIT ${indexfrom}, ${counts} ` );
  ctx.body = {
    result: 200,
    list:results,
    message: "查询成功"
  };
}

exports.update = async(ctx) => {
  let product_id = ctx.request.body.product_id || '';
  let vendor_id = ctx.request.body.vendor_id || '';
  let product_name = ctx.request.body.product_name || '';
  let update_time = moment().format('YYYY-MM-DD HH:mm:ss');
  let model_type_id = ctx.request.body.model_type_id || '';
  let model_name = ctx.request.body.model_name || '';
  let model_property = ctx.request.body.model_property || {};
  let product_type = ctx.request.body.product_type || '';
  let icon_id = ctx.request.body.icon_id || '';

  try {
    let selectRows = await mysql.query(`SELECT * FROM product WHERE product_id = ?`, product_id);
    if (selectRows.length > 0) {
      let update_date = assign({},selectRows[0], ctx.request.body, { update_time });
      console.log(selectRows[0]);
      let ret = await mysql.query(`UPDATE product SET ? WHERE product_id = ${product_id}`, update_date);
      console.log(ret);
      if (!ret || ret.insertId === -1) {
        ctx.body = {
          result: 202,
          message: "更新失败"
        };
      } else {
        ctx.body = {
          result: 200,
          message: "更新成功"
        };
      }
    } else {
      ctx.body = {
        result: 203,
        message: "对象不存在或者已删除"
      };
    }
  }catch (e) {
    ctx.body = {
      result: 204,
      message: "更新失败"+e.toString()
    };
  }

}

exports.delete = async(ctx) => {
  let request_body = ctx.request.body;
  let product_id = ctx.params.product_id;

  let results = await mysql.query(`delete from product  WHERE product_id = ${product_id}`);

  if (results.length > 0 || results){
    let newResult = await mysql.query(`select * from  product`);
    ctx.body = {
      return: 200,
      results: newResult,
      message: "删除product成功"
    };
  }
  else {
    ctx.body = {
      return: 0,
      results,
      message: "更新失败"
    };
  }

}

